package com.example.nacos.controller;

import com.example.nacos.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置管理控制器
 */
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
    
    @Autowired
    private AppConfig appConfig;
    
    @Value("${app.name:default-app}")
    private String appName;
    
    @Value("${app.version:1.0.0}")
    private String appVersion;
    
    /**
     * 获取应用配置
     */
    @GetMapping("/app")
    public Map<String, Object> getAppConfig() {
        Map<String, Object> result = new HashMap<>();
        result.put("name", appConfig.getName());
        result.put("version", appConfig.getVersion());
        result.put("description", appConfig.getDescription());
        result.put("database", appConfig.getDatabase());
        return result;
    }
    
    /**
     * 获取简单配置值
     */
    @GetMapping("/simple")
    public Map<String, String> getSimpleConfig() {
        Map<String, String> result = new HashMap<>();
        result.put("appName", appName);
        result.put("appVersion", appVersion);
        return result;
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, String> health() {
        Map<String, String> result = new HashMap<>();
        result.put("status", "UP");
        result.put("app", appName);
        result.put("version", appVersion);
        return result;
    }
}

